// pages/independent/course_section/course_section.js
import SectionTool from "../../../utils/SectionTool.js"
import NoteTool from "../../../utils/NoteTool.js"
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		sectionInfo:{},
		tabBar:[
			{
				"name":"我的笔记",
				"sign":1
			},
			{
				"name":"他人笔记",
				"sign":2
			}
		],
		signOfTabBar:1,
		numOfNote:12,
		bt_add_className:[
			"_myNote_add_null",
			"_myNote_add_get"
		],
		noteList:{},
		isShowInput:false,
		number1:"",
		number2:""
	},

	setValue1(e) {
		this.setData({
		  	number1: e.detail.value
		})
	},
	setValue2(e) {
		this.setData({
		  	number2: e.detail.value
		})
	},
	cancel(){
		this.setData({
			isShowInput: false,
		})
	},
	confirmAcceptance(){
		this.setData({
			isShowInput: false,
		})
		console.log(this.data.number1);
		console.log(this.data.number2);
		let addNote = NoteTool.addNote(this.data.sectionInfo.id,this.data.number1,this.data.number2);
		console.log(this.data.sectionInfo.id);
		addNote.then((res)=>{
			console.log(res);	
			this.onShow()
			wx.showToast({
				title: '添加成功',
				icon: 'success',
				duration: 3000
			})
		})
		
	},

	changeList(e) {
		let sign = e.currentTarget.dataset.sign;
		this.setData({
			signOfTabBar:sign
		})
		console.log(this.data.signOfTabBar);
		switch (this.data.signOfTabBar) {
			case 1:
				this.getSectionNoteMine()
				break;
		
			default:
				this.getSectionNote()
				break;
		}
	},

	goToNode(e){
		let id = e.currentTarget.dataset.noteId;
		wx.navigateBack({
		})
	},

	videoEnded(){
		if(this.data.sectionInfo.status != 1){
			let setFinish = SectionTool.setSectionStatus(this.data.sectionInfo.id,1);
			setFinish.then((res)=>{
				console.log(res);
			})
		}
		
	},

	getSectionNote(){
		console.log(1);
		let note_list = NoteTool.getSectionNote(this.data.sectionInfo.id,1,1);
		note_list.then((res)=>{
			this.setData({
				noteList: res.data
			})
			console.log(444);
			console.log(this.data.noteList);
		})
	},
	openNote(e){
		let t = e.currentTarget.dataset.item;
		console.log(t);
		let str = JSON.stringify(t);
		let sign = this.data.signOfTabBar;
		wx.navigateTo({
		  	url: '/pages/independent/note_open/note_open?str='+str+"&sign="+sign,
		})
	},
	
	addNote(){
		this.setData({
			isShowInput:true
		})
	},

	getSectionNoteMine(){
		console.log(0);
		let note_list = NoteTool.getSectionNote(this.data.sectionInfo.id,0,1);
		note_list.then((res)=>{
			this.setData({
				noteList: res.data
			})
			console.log(444);
			console.log(this.data.noteList);
		})
	},

	deleteNote(){
		// let delNote = NoteTool.delNote(1);
		// delNote.then((res)=>{
		// 	console.log(res);
		// })
		NoteTool.delNote(1);
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		let sectionInfo = JSON.parse(options.sectionInfo)
		console.log(sectionInfo);
		this.setData({
			sectionInfo: sectionInfo
		})
		wx.setNavigationBarTitle({
			title: this.data.sectionInfo.title
		})

		if(sectionInfo.status == 0){
			let setStudying = SectionTool.setSectionStatus(this.data.sectionInfo.id,2);
			setStudying.then((res)=>{
				console.log(res);
			})
		}
		console.log(this.data.sectionInfo);
		this.getSectionNoteMine();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		switch (this.data.signOfTabBar) {
			case 1:
				this.getSectionNoteMine()
				break;
		
			default:
				this.getSectionNote()
				break;
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})